package com.noah.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.noah.model.SimpleUser;
import com.noah.services.LoginServices;

/**
 * Servlet implementation class SimpleLoginServlet
 */
@WebServlet("/SimpleLogin")
public class SimpleLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SimpleLoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String user = request.getParameter("userName");
		String pwd = request.getParameter("password");
		LoginServices lsv = new LoginServices();
		boolean res = lsv.checkLogin(user, pwd);
		SimpleUser su = lsv.getUserDetails(user);
		// ------------Cach dung response.sendRedirect
		/*
		 * HttpSession se = request.getSession(); se.setAttribute("user", su);
		 * if (res) { response.sendRedirect("LoginOK.jsp"); } else {
		 * response.sendRedirect("SimpleLogin.jsp"); }
		 */

		// ----------- Cach dung RequestDispatcher
		request.setAttribute("user", su);
		if (res) {
			//RequestDispatcher rd = request.getRequestDispatcher("LoginOK.jsp");
			RequestDispatcher rd = request.getRequestDispatcher("LoginOKK.jsp");
			rd.forward(request, response);
		} else {
			response.sendRedirect("SimpleLogin.jsp");
		}

	}

}
